package com.eques.util

import com.eques.icvss.api.ICVSSInstanceCreator
import com.eques.icvss.api.ICVSSListener
import com.eques.icvss.api.ICVSSUserInstance
import com.eques.icvss.core.iface.ICVSSRoleType
import com.eques.iot.api.IOTListener
import kotlin.concurrent.Volatile

/**
 * ICVSS框架初始化类
 */
class ICVSSUserModule private constructor(listener: ICVSSListener, iotListener: IOTListener) {
    var icvss: ICVSSUserInstance? = null
        private set

    init {
        synchronized(ICVSSUserModule::class.java) {
            icvss =
                ICVSSInstanceCreator.createUserInstance(ICVSSRoleType.CLIENT, listener, iotListener)
        }
    }

    fun closeIcvss() {
        synchronized(ICVSSUserModule::class.java) {
            if (null != mUserICVSSModule) {
                mUserICVSSModule = null
            }
            if (null != icvss) {
                ICVSSInstanceCreator.close()
                icvss = null
            }
        }
    }

    companion object {
        private const val TAG = "UserICVSSModule"

        @Volatile
        private var mUserICVSSModule: ICVSSUserModule? = null
        @JvmStatic
        fun getInstance(listener: ICVSSListener, iotListener: IOTListener): ICVSSUserModule? {
            if (null == mUserICVSSModule) {
                synchronized(ICVSSUserModule::class.java) {
                    if (null == mUserICVSSModule) {
                        mUserICVSSModule = ICVSSUserModule(listener, iotListener)
                    }
                }
            }
            return mUserICVSSModule
        }
    }
}
